package com.reremouse.rerebbs.web.mb;

import com.reremouse.rerebbs.dao.IBbsUserDAO;
import com.reremouse.rerebbs.dao.ISystemConfigDAO;
import com.reremouse.rerebbs.model.BbsUser;
import com.reremouse.service.EmailService;
import com.reremouse.util.JsfHelper;
import com.reremouse.util.MD5;
import com.reremouse.util.SpringHelper;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

@ManagedBean
@ViewScoped
public class Register
        implements Serializable {
    BbsUser user = new BbsUser();
    IBbsUserDAO bbsUserDAO = (IBbsUserDAO) SpringHelper.getSpringBean("BbsUserDAO");
    ISystemConfigDAO scd = (ISystemConfigDAO) SpringHelper.getSpringBean("SystemConfigDAO");
    EmailService emailService = (EmailService) SpringHelper.getSpringBean("EmailService");
    String siteName = (String) SpringHelper.getSpringBean("siteName");
    String pwd_re = "";
    String authStr = "";
    String id_temp = new Long(System.currentTimeMillis()).toString();
    String urnTip = "";
    boolean urnOk = true;
    String emailTip = "";
    boolean emailOk = true;
    boolean accept = true;

    public String getId_temp() {
        return this.id_temp;
    }

    public void setId_temp(String id_temp) {
        this.id_temp = id_temp;
    }

    public String getAuthStr() {
        return this.authStr;
    }

    public void setAuthStr(String authStr) {
        this.authStr = authStr;
    }

    public boolean isAccept() {
        return this.accept;
    }

    public void setAccept(boolean accept) {
        this.accept = accept;
    }

    public String getPwd_re() {
        return this.pwd_re;
    }

    public void setPwd_re(String pwd_re) {
        this.pwd_re = pwd_re;
    }

    public BbsUser getUser() {
        return this.user;
    }

    public void setUser(BbsUser user) {
        this.user = user;
    }

    public boolean isUrnOk() {
        return this.urnOk;
    }

    public void setUrnOk(boolean urnOk) {
        this.urnOk = urnOk;
    }

    public String getUrnTip() {
        return this.urnTip;
    }

    public void setUrnTip(String urnTip) {
        this.urnTip = urnTip;
    }

    public boolean isEmailOk() {
        return this.emailOk;
    }

    public void setEmailOk(boolean emailOk) {
        this.emailOk = emailOk;
    }

    public String getEmailTip() {
        return this.emailTip;
    }

    public void setEmailTip(String emailTip) {
        this.emailTip = emailTip;
    }

    public void onChangeUrn() {
        String urn = this.user.getUsername();
        if ((urn != null) && (!"".equals(urn))) {
            BbsUser bu = this.bbsUserDAO.findBbsUserByUrn(urn);
            if (bu != null) {
                this.urnOk = false;
                this.urnTip = "此用户名已存在！";
            } else {
                this.urnOk = true;
                this.urnTip = "this user can use";
            }
        }
    }

    public void onChangeEmail() {
        String email = this.user.getEmail();
        if ((email != null) && (!"".equals(email))) {
            BbsUser bu = this.bbsUserDAO.findBbsUserByEmail(email);
            if (bu != null) {
                this.emailOk = false;
                this.emailTip = "email exist";
            } else {
                this.emailOk = true;
                this.emailTip = "此邮箱可以注册！";
            }
        }
    }

    public String refreshAuth() {
        this.id_temp = new Long(System.currentTimeMillis()).toString();
        return null;
    }

    public String reg_ok() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesMessage fm = new FacesMessage();
        FacesContext fc = FacesContext.getCurrentInstance();
        fm.setSeverity(FacesMessage.SEVERITY_ERROR);
        if ((this.user.getUsername().equals("")) || (this.user.getPassword().equals("")) || (this.pwd_re.equals(""))) {
            fm.setSummary("所有输入框不能为空！");
            fc.addMessage("", fm);
            return null;
        }
        if (!this.user.getPassword().equals(this.pwd_re)) {
            fm.setSummary("two pass not same");
            fc.addMessage("", fm);
            return null;
        }
        HttpSession s = (HttpSession) fc.getExternalContext().getSession(true);
        String str = (String) s.getAttribute("rand");
        if (!this.authStr.equals(str)) {
            fm.setSummary("validate id not success");
            fc.addMessage("", fm);
            return null;
        }
        BbsUser us = this.bbsUserDAO.findBbsUserByUrn(this.user.getUsername());
        if (us != null) {
            fm.setSummary("此用户已经存在！");
            fc.addMessage("", fm);
            return null;
        }
        BbsUser us0 = this.bbsUserDAO.findBbsUserByEmail(this.user.getEmail());
        if (us0 != null) {
            fm.setSummary("this email exist");
            fc.addMessage("", fm);
            return null;
        }
        String ip = request.getRemoteAddr();
        this.user.setRegIp(ip);
        this.user.setLastIp(ip);
        if (this.scd.getSystemConfig().getEmailValidation().booleanValue()) {
            HttpServletRequest req = JsfHelper.getRequest();
            String host = req.getLocalAddr();
            int port = req.getLocalPort();
            String sign = MD5.MD5Encode(new Long(this.user.getId().longValue()).toString());
            String mailbody = "点击<a href='http://" + host + ":" + port + req.getContextPath() + "/servlet/RegisterActivate?urn=" + this.user.getUsername() + "&amp;sign=" + sign + "'>here</a>激活帐号";


            ServletContext sc = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
            boolean b = this.emailService.sendEmail(this.user.getEmail(), this.siteName + "system id active message", mailbody);
            if (!b) {
                fm.setSummary("the email can't send,so can't register");
                fc.addMessage("", fm);
                return null;
            }
            this.user.setActivated(Boolean.valueOf(false));
            this.bbsUserDAO.addBbsUser(this.user);
            return "RegEmailOut";
        }
        this.bbsUserDAO.addBbsUser(this.user);
        ClientSession cs = (ClientSession) JsfHelper.getBean(FacesContext.getCurrentInstance(), "clientSession");

        cs.setIfLogin(true);
        cs.setUsr(this.user);
        return "ZoneList?faces-redirect=true";
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.Register

 * JD-Core Version:    0.7.0.1

 */